package cn.gson.boot.controller;

import cn.gson.boot.model.pojos.*;
import cn.gson.boot.model.service.HeTongService;
import cn.gson.boot.model.service.WeiXiuGongDanService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/wxgd")
public class WeiXiuGongDanController {
    @Autowired
    WeiXiuGongDanService weiXiuGongDanService;
    @Autowired
    HeTongService heTongService;


    //根据客户查看联系人
    @RequestMapping("/cklianxiren")
    @ResponseBody
    public List<Contacts> cklianxiren(Integer kId){
        System.err.println("KKKKKKKK:"+kId);
        return weiXiuGongDanService.cklianxiren(kId);
    }

    //根据选择的客户查询客户所有的订单
    @RequestMapping("/wxgdSelectYwcDingDanBykId")
    @ResponseBody
    /*public List<Dingdan> ckdingdan(Integer kId){
        return heTongService.ckdingdan(kId);
    }*/
    //{维修工单}根据选择的客户查询客户所有已完成的订单
    public List<Dingdan> wxgdSelectYwcDingDanBykId(Integer kId){
        return heTongService.wxgdSelectYwcDingDanBykId(kId);
    }

    //根据选择的订单查到此订单详情表里面的产品
    @RequestMapping("/ckDingDanChanPin")
    @ResponseBody
    public List<Productt> ckDingDanChanPin(Integer ddId){

        return heTongService.ckDingDanChanPin(ddId);
    }

    //根据产品查询订单详情的信息
    @RequestMapping("/ckDingDanChanXiangQinByChanPin")
    @ResponseBody
    public Dingdanxiangqing ckDingDanChanXiangQinByChanPin(Integer producttId,Integer ddId){
        return heTongService.ckDingDanChanXiangQinByChanPin(producttId,ddId);
    }

    //查看所有部门(维修部)
    @RequestMapping("/ckAllBuMen")
    @ResponseBody
    public List<Dept> ckAllBuMen(){
        return weiXiuGongDanService.ckAllBuMen();
    }

    //新增维修工单
    @RequestMapping("/addWeiXiuGongDan")
    @ResponseBody
    public void addWeiXiuGongDan(@RequestBody Weixiugongdan weixiugongdan){
        System.err.println("contr:"+weixiugongdan);
       weiXiuGongDanService.addWeiXiuGongDan(weixiugongdan);
    }

   //查看所有维修工单
    @RequestMapping("/selectAllWeiXiugd")
    @ResponseBody
    public Map<String,Object> selectAllWeiXiugd(String kName, String gdHao, String time,
                                                Integer pageNumm, Integer pageSizee, HttpSession session){
        Page<Object> objects = PageHelper.startPage(pageNumm, pageSizee);
        String time3 = null;
        if(time != null && time != "" && !time.equals("")){
            StringBuffer time1 = new StringBuffer(time.substring(0,10));
            System.err.println("tim1:"+time1);
            int ti = Integer.parseInt(time1.substring(8))+1;
            System.err.println("ti:"+ti);
            String tistr = toString().valueOf(ti);
            if(toString().valueOf(ti).length() == 1){
                tistr = 0 + toString().valueOf(ti);
            }
            StringBuffer time2 = new StringBuffer(time1.replace(8,time1.length(), tistr));
            System.err.println("tim2:"+time2);
            time3 = time2.toString();
        }

        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        String empName = null;
        if(jueNameList.indexOf("boss")>-1 ||jueNameList.indexOf("维修部长")>-1  ){
            empName = null;
        }else{
            empName = us.getEmpName();
        }
        List<Weixiugongdan> list = weiXiuGongDanService.selectAllWeiXiugd(kName,gdHao,time3,empName);

        Map<String,Object> map = new HashMap();
        map.put("total", objects.getTotal());
        map.put("list", list);
        return map;
    }

    //移除工单
    @RequestMapping("/yichuWXGD")
    @ResponseBody
    public void yichuWXGD(Integer gdId){
        weiXiuGongDanService.yichuWXGD(gdId);
    }

    //根据联系人id查到联系人信息
    @RequestMapping("/ckLianXiRenBycId")
    @ResponseBody
    public Contacts ckLianXiRenBycId(Integer cId){
        return  weiXiuGongDanService.ckLianXiRenBycId(cId);
    }

    //根据订单id查到订单信息
    @RequestMapping("/ckDingDanByddId")
    @ResponseBody
    public Dingdan ckDingDanByddId(Integer ddId){
        return  heTongService.ckDingDanByddId(ddId);
    }

    //根据部门id查到部门信息
    @RequestMapping("/ckBuMenBydeptId")
    @ResponseBody
    public Dept ckBuMenBydeptId(Integer deptId){
        return  weiXiuGongDanService.ckBuMenBydeptId(deptId);
    }

    //根据产品id查到产品信息
    @RequestMapping("/ckCPxx")
    @ResponseBody
    public Productt ckCPxx(Integer producttId){
        return weiXiuGongDanService.ckCPxx(producttId);
    }



    //新增检测
    @RequestMapping("/addJianCe")
    @ResponseBody
    public void addJianCe(@RequestBody Wxjiance wxjiance){
        weiXiuGongDanService.addJianCe(wxjiance);
    }

    //根据维修工单id查看维修检测
    @RequestMapping("/seleceWxjcByGdId")
    @ResponseBody
    public Wxjiance seleceWxjcByGdId(Integer gdId){
        return weiXiuGongDanService.seleceWxjcByGdId(gdId);
    }

    //查看所有的员工
    @RequestMapping("/selectAllEmpp")
    @ResponseBody
    public List<User> selectAllEmpp(){
        return weiXiuGongDanService.selectAllEmpp();
    }

    //检测完成
    @RequestMapping("/jianceWanCheng")
    @ResponseBody
    public void jianceWanCheng(Integer gdId){
        weiXiuGongDanService.jianceWanCheng(gdId);
    }

    //新增客户确定
    @RequestMapping("/addkhQueDing")
    @ResponseBody
    public void addkhQueDing(@RequestBody Wxqueding Wxqueding){
        weiXiuGongDanService.addkhQueDing(Wxqueding);
    }

    //根据维修工单id查看客户确定
    @RequestMapping("/selectKHqueDing")
    @ResponseBody
    public Wxqueding selectKHqueDing(Integer gdId){
        return weiXiuGongDanService.selectKHqueDing(gdId);
    }

    //确定维修quedingWeiXiu
    @RequestMapping("/quedingWeiXiu")
    @ResponseBody
    public void quedingWeiXiu(Integer gdId) {
        weiXiuGongDanService.quedingWeiXiu(gdId);
    }

    //确定不维修quedingWeiXiu
    @RequestMapping("/quedingBuWeiXiu")
    @ResponseBody
    public void quedingBuWeiXiu(Integer gdId) {
        weiXiuGongDanService.quedingBuWeiXiu(gdId);
    }

    //根据维修工单id查看维修过程
    @RequestMapping("/seleceWxgcByGdId")
    @ResponseBody
    public Wxguocheng seleceWxgcByGdId(Integer gdId){
        return weiXiuGongDanService.seleceWxgcByGdId(gdId);
    }

    //新增维修过程
    @RequestMapping("/addWeiXiuGuoCheng")
    @ResponseBody
    public void addWeiXiuGuoCheng(@RequestBody Wxguocheng wxguocheng){
        weiXiuGongDanService.addWeiXiuGuoCheng(wxguocheng);
    }

//修改维修工单的进度为维修中
    @RequestMapping("/kaiShiWeiXiu")
    @ResponseBody
    public void kaiShiWeiXiu(Integer gdId){
        weiXiuGongDanService.kaiShiWeiXiu(gdId);
    }

    //修改维修工单的进度为修理完成
    @RequestMapping("/weiXiuWanCheng")
    @ResponseBody
    public void weiXiuWanCheng(Integer gdId) {
        weiXiuGongDanService.weiXiuWanCheng(gdId);
    }

    //根据工单id查看维修交付
    @RequestMapping("/selectwxJiaoFu")
    @ResponseBody
    public Wxjiaofu selectwxJiaoFu(Integer gdId){
        return weiXiuGongDanService.selectwxJiaoFu(gdId);
    }

    //新增维修交付
    @RequestMapping("/jiaoFuGeiKeHu")
    @ResponseBody
    public void jiaoFuGeiKeHu(@RequestBody Wxjiaofu wxjiaofu){
        weiXiuGongDanService.jiaoFuGeiKeHu(wxjiaofu);
    }

    //根据维修工单id查看维修收款
    @RequestMapping("/seleceWxshoukuanByGdId")
    @ResponseBody
    public List<Wxshoukuan> seleceWxshoukuanByGdId(Integer gdId){
        return weiXiuGongDanService.seleceWxshoukuanByGdId(gdId);
    }

    //查看剩余要汇款的金额
    @RequestMapping("/selectShengYvMoney")
    @ResponseBody
    public double selectShengYvMoney(Integer gdId){
        //应回款金额
        double ygaihmoney = weiXiuGongDanService.selectWeiXiugdByGdId(gdId).getWeixiuMoney().doubleValue();
        //已回金额
        double yjinghmoney = 0;
        //根据维修工单id查看此维修工单的所有维修收款
        List<Wxshoukuan> sk = weiXiuGongDanService.seleceWxshoukuanByGdId(gdId);
        for(int i=0;i<sk.size();i++){
            yjinghmoney = yjinghmoney + sk.get(i).getSkMoney().doubleValue();
        }
        //剩余
        double shengyvMoney = ygaihmoney - yjinghmoney;
        return shengyvMoney;
    }

    //新增维修收款
    @RequestMapping("/addHuiKuang")
    @ResponseBody
    public void addHuiKuang(@RequestBody Wxshoukuan wxshoukuan){
        weiXiuGongDanService.addHuiKuang(wxshoukuan);
    }


    //维修工单主控台==================================================================
    public String empName(HttpSession session){
        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        String empName = null;
        if(jueNameList.indexOf("boss")>-1 ||jueNameList.indexOf("维修部长")>-1  ){
            empName = null;
        }else{
            empName = us.getEmpName();
        }
        return empName;
    }

    //查看待检测维修工单
    @RequestMapping("/selectdaiJianCe")
    @ResponseBody
    public List<Weixiugongdan> selectdaiJianCe(String tj,HttpSession session){
        return weiXiuGongDanService.selectdaiJianCe(tj,empName(session));
    }
    //查看待与客户确认维修工单
    @RequestMapping("/selectdaiYvKeHuQueRen")
    @ResponseBody
    public List<Weixiugongdan> selectdaiYvKeHuQueRen(String tj,HttpSession session){

        return weiXiuGongDanService.selectdaiYvKeHuQueRen(tj,empName(session));
    }
    //查看待维修维修工单
    @RequestMapping("/selectdaiWeiXiu")
    @ResponseBody
    public List<Weixiugongdan> selectdaiWeiXiu(String tj,HttpSession session){
        return weiXiuGongDanService.selectdaiWeiXiu(tj,empName(session));
    }
    //查看维修中维修工单
    @RequestMapping("/selectweiXiuZhong")
    @ResponseBody
    public List<Weixiugongdan> selectweiXiuZhong(String tj,HttpSession session){
        return weiXiuGongDanService.selectweiXiuZhong(tj,empName(session));
    }
    //查看修理完成维修工单
    @RequestMapping("/selectxiuLiWanCheng")
    @ResponseBody
    public List<Weixiugongdan> selectxiuLiWanCheng(String tj,HttpSession session){
        return weiXiuGongDanService.selectxiuLiWanCheng(tj,empName(session));
    }
    //查看已交付维修工单
    @RequestMapping("/selectyiJiaoFu")
    @ResponseBody
    public List<Weixiugongdan> selectyiJiaoFu(String tj,HttpSession session){
        return weiXiuGongDanService.selectyiJiaoFu(tj,empName(session));
    }
    //查看（未尽收款）已交付维修工单
    @RequestMapping("/selectyiJiaoFu22")
    @ResponseBody
    public List<Weixiugongdan> selectyiJiaoFu22(String tj,HttpSession session){
        return weiXiuGongDanService.selectyiJiaoFu22(tj,empName(session));
    }
    //查看已完成维修工单
    @RequestMapping("/selectyiWanCheng")
    @ResponseBody
    public List<Weixiugongdan> selectyiWanCheng(String tj,HttpSession session){

        return weiXiuGongDanService.selectyiWanCheng(tj,empName(session));
    }

    //查看待交付维修工单
    @RequestMapping("/selectdaiJiaoFu")
    @ResponseBody
    public List<Weixiugongdan> selectdaiJiaoFu(String tj,HttpSession session){
        return weiXiuGongDanService.selectdaiJiaoFu(tj,empName(session));
    }

//=============================================================================================================

    //根据工单id查看工单详情
    @RequestMapping("/selectgdXQbygdId")
    @ResponseBody
    public List<Wxgdxiangqing> selectgdXQbygdId(Integer gdId){
        return weiXiuGongDanService.selectgdXQbygdId(gdId);
    }


    @RequestMapping("/weixiuEmp")
    @ResponseBody
    public List<User> weixiuEmp(){
        return weiXiuGongDanService.weixiuEmp();
    }
    @RequestMapping("/jianceEmp")
    @ResponseBody
    public List<User> jianceEmp(){
        return weiXiuGongDanService.jianceEmp();
    }
}
